package com.wd.live.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.wd.live.R;
import com.wd.live.base.BaseActivity;
import com.wd.live.base.BaseViewModel;
import com.wd.live.bean.GiftBean;
import com.wd.live.databinding.ActivityGiftListBinding;
import com.wd.live.viewmodel.GiftListViewModel;

import java.util.List;

public class GiftListActivity extends BaseActivity<GiftListViewModel, ActivityGiftListBinding> {
    String name[]={"我收到的","我送出的"};
    private int o=1;
    private int page=1;
    private int position;

    @Override
    protected void initData() {
        databind.HBsmart.setRefreshHeader(new ClassicsHeader(this));
        databind.HBsmart.setRefreshFooter(new ClassicsFooter(this));
        databind.backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if(databind.tab.getTabCount()==0){
            for (String s : name) {
                databind.tab.addTab(databind.tab.newTab().setText(s));
            }
        }
        GetGiftList(o);
        databind.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                position = tab.getPosition();
                GetGiftList(position+1);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void GetGiftList(int o){
        viewmodel.getList(o,page).observe(GiftListActivity.this, new Observer<GiftBean>() {
            @Override
            public void onChanged(GiftBean giftBean) {
                GiftBean.ResultBean result = giftBean.getResult();
                List<?> list = result.getGiftRecordList();
                List<?> totalList = result.getGiftTotalList();
                if(list.size()==0||totalList.size()==0){
                    databind.giftImgLayout.setVisibility(View.GONE);
                    databind.giftListLayout.setVisibility(View.GONE);
                    databind.nullImg.setImageResource(R.mipmap.nogifts);
                    if(o==1){
                        databind.nullName.setText("暂时没有收到礼物");
                    }else{
                        databind.nullName.setText("暂时没有送出礼物");
                    }
                    databind.noGift.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_gift_list;
    }

    @Override
    public void onChanged(Object o) {

    }
}